package com.example.file.win;

import com.example.file.dao.DataBase;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.text.AbstractDocument;
import java.awt.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class LeftRightLayoutExample {

    private JFrame frame;
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JButton findButton;
    private JButton noteButton;
    private JPanel findPanel;
    private JPanel notePanel;
    private CardLayout cardLayout;


    private JRadioButton[] radioButtons;
    private List<String> name;
    ArrayList<JCheckBox> jCheckBoxes;

    {
        name = DataBase.selectType();
        radioButtons = new JRadioButton[name.size()];
        jCheckBoxes = new ArrayList<>();
        for (String s : name) {
            jCheckBoxes.add(new JCheckBox(s));
        }
    }

    public LeftRightLayoutExample() {
        frame = new JFrame("超越一切！");
//        设置可关闭
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        窗口大小
        frame.setSize(800, 600);
//        窗口锁定
        frame.setResizable(false);
//        左侧画板
        leftPanel = new JPanel();
//        右侧画板
        rightPanel = new JPanel();
//        左侧画板设置为垂直结构
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
//        leftPanel.setPreferredSize(new Dimension(80, 300));
        // 设置卡片布局，用于在右侧面板中切换不同的组件
        cardLayout = new CardLayout();
//        右侧设置为动态画板
        rightPanel.setLayout(cardLayout);

        // 创建“查找”和“记错”按钮
        findButton = new JButton("查找");
        noteButton = new JButton("记错");

        // 查找时的组件
        findPanel = new JPanel();
//        查找画板设置为垂直结构
        findPanel.setLayout(new BoxLayout(findPanel, BoxLayout.Y_AXIS));
//       设置垂直间距
        findPanel.add(Box.createVerticalStrut(0));
//        查找第一行查找框和文本
        JPanel jPanel0 = new JPanel();
//        查找第二行类型信息
        JPanel jPanel = new JPanel();
//        查找第三行表格画板
        JPanel comp4 = new JPanel();
//        表头
        String[] columnNames = {"类型", "标题", "内容"};
//        表内容
        Object[][] data = {
                {"类型", "标题", "内容"},
                {"Jane", 30, "Female"},
                {"Bob", 40, "Male"}
        };
//          表创建
        DefaultTableModel model = new DefaultTableModel(data, columnNames);

        // 创建JTable并设置数据模型
        JTable table = new JTable(model);
//        设置表大小
        table.setPreferredSize(new Dimension(600, 200)); // 设置首选大小
        table.setMinimumSize(new Dimension(600, 200));
        TableColumnModel columnModel = table.getColumnModel();

// 获取第一列的TableColumn对象
        TableColumn firstColumn = columnModel.getColumn(0);
        firstColumn.setPreferredWidth(0);
// 调整表格的列宽以适应新设置
        table.doLayout();
        table.revalidate();
        table.repaint();
//        标题搜索框
        JTextField comp1 = new JTextField(20);
//        搜索按钮
        JButton sou = new JButton("搜索");
//        添加组件
        jPanel0.add(new JLabel("查找内容:"));
        jPanel0.add(comp1);
        jPanel0.add(sou);
//      添加类型信息
        for (JCheckBox jCheckBox : jCheckBoxes) {
            jPanel.add(jCheckBox);
        }
        comp4.add(table);

//        查找画板添加内容
        findPanel.add(jPanel0);
        findPanel.add(jPanel);
        findPanel.add(comp4);
//      添加空画板用来布局填充
        findPanel.add(new JPanel());
        findPanel.add(new JPanel());
        findPanel.add(new JPanel());
        // 记错时的组件
        notePanel = new JPanel();
        notePanel.setLayout(new BoxLayout(notePanel, BoxLayout.Y_AXIS));
//        记错第一行标题
        JPanel jPanel1 = new JPanel();
//        记错第二行类型
        JPanel jPanel2 = new JPanel();
//        记错第三行内容
        JPanel jPanel3 = new JPanel();
//        记错第四行按钮
        JPanel jPanel4 = new JPanel();
//        标题框
        JTextField comp = new JTextField(20);


        ButtonGroup buttonGroup = new ButtonGroup();
        for (int i = 0; i < radioButtons.length; i++) {
            radioButtons[i] = new JRadioButton(name.get(i));
            buttonGroup.add(radioButtons[i]);
            // 默认选中第一个单选按钮
            if (i == 0) {
                radioButtons[i].setSelected(true);
            }
        }
//        内容框
        JTextArea textArea = new JTextArea(10, 30);
        textArea.setLineWrap(true); // 启用自动换行
        int maxCharacters = 200;
        textArea.setText("最多添加" + maxCharacters + "个字符");
        ((AbstractDocument) textArea.getDocument()).setDocumentFilter(new CharacterLimitFilter(maxCharacters));

        JButton submit = new JButton("提交");
        JButton del = new JButton("置空");
//        添加展示信息
        jPanel1.add(new JLabel("标题:"));
        jPanel1.add(comp);
        for (JRadioButton radioButton : radioButtons) {
            jPanel2.add(radioButton);
        }
        jPanel3.add(new JLabel("内容:"));
        jPanel3.add(textArea);
        jPanel4.add(submit);
        jPanel4.add(del);
//        添加到记错画板
        notePanel.add(jPanel1);
        notePanel.add(jPanel2);
        notePanel.add(jPanel3);
        notePanel.add(jPanel4);
        // 将组件添加到右侧面板中
        rightPanel.add(findPanel, "find");
        rightPanel.add(notePanel, "note");

        // 将按钮添加到左侧面板中
        leftPanel.add(findButton);
        leftPanel.add(noteButton);

        // 创建水平分割面板，将左右面板添加到其中
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
        splitPane.setDividerLocation(80); // 设置分割线的位置

        // 添加按钮的事件监听器
        findButton.addActionListener(e -> cardLayout.show(rightPanel, "find"));

        noteButton.addActionListener(e -> cardLayout.show(rightPanel, "note"));
//        点击搜索的监听操作
        sou.addActionListener(e -> {
            int rowCount = table.getRowCount();
            for (int i = 1; i < rowCount; i++) {
//                保留标题删除其他内容
                model.removeRow(1);
            }
//            获取选择查找的类型
            List<String> tableList = new ArrayList<>();
            for (JCheckBox jCheckBox : jCheckBoxes) {
                if (jCheckBox.isSelected()) {
                    tableList.add(jCheckBox.getText());
//                    System.out.println(jCheckBox.getText());
                }
            }
//            模糊搜索的信息
            String message = comp1.getText();
//            查找方法
            List<List<String>> maps = DataBase.selectMessage(tableList, message);
//            信息添加到表格
            for (List<String> map : maps) {
                Object[] objects = map.toArray();
                model.addRow(objects);
            }
//            调整
            for (int i = 0; i < table.getRowCount(); i++) {
                table.setRowHeight(i, 50); // 设置每一行的高度
            }
        });
//        记错提交监听
        submit.addActionListener(e -> {
//            获取输入内容
            String info = textArea.getText();
            String head = comp.getText();
            String type = null;
            Enumeration<AbstractButton> enumeration = buttonGroup.getElements();// 获取Enumeration对象
            while (enumeration.hasMoreElements()) {
                AbstractButton button = enumeration.nextElement();
                // 对button进行所需的操作
                if (button.isSelected()) {
                    // 输出选中按钮的文本
                    type = button.getText();
                    System.out.println("Selected button: " + type);
                    break;
                }
            }
//            数据校验
            if (type == null) {
                JOptionPane.showMessageDialog(null, "错误");
                return;
            }
            if ("".equals(head)) {
                JOptionPane.showMessageDialog(null, "标题为空");
                return;
            }
            if ("".equals(info)) {
                JOptionPane.showMessageDialog(null, "内容为空");
                return;
            }
//            数据库插入
            boolean insert = DataBase.insert(head, info, type);
//            添加提示
            if (insert) {
                JOptionPane.showMessageDialog(null, "添加成功");
                comp.setText("");
                textArea.setText("");
            }else {
                JOptionPane.showMessageDialog(null, "添加失败");
            }
        });
//        置空按钮监听
        del.addActionListener(e -> {
            comp.setText("");
            textArea.setText("");
        });

        // 创建垂直布局，将分割面板添加到其中，然后添加到框架中
        JPanel contentPanel = new JPanel(new BorderLayout());
        contentPanel.add(splitPane, BorderLayout.CENTER);
        frame.add(contentPanel);

        // 显示窗口
        frame.setVisible(true);
    }
}

